<template>
	<div class="evidence-unit-casePrint-container">
		<el-dialog title="物品新增/编辑" v-model="isShowDialog" width="1200px">
			<AddEvi v-if="isShowDialog" ref="refAddEvi" :formInline="formInline" :options="options" />
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="small">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="small" :disabled="!isSameOrgan">保 存</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { reactive, toRefs, ref } from 'vue';
import AddEvi from '/@/views/evidence/add/component/addEvidence.vue';
import { initDic } from '/@/common';
import { getEvidence } from '/@/api/evidence/index';
import { save } from '/@/api/evidence/index';
import { ElMessage } from 'element-plus';
export default {
	name: 'editCase',
	components: { AddEvi },
	props: { 
		caseId: String,
		isSameOrgan:{
			type:Boolean,
			default:true
		}
	},
	emits: ['reload'],
	setup(props: any, { emit }) {
		//备注空值
		var _bakFormInline = {
			caseId: Number(props.caseId),
			name: '',
			class: '',
			rFID: '',
			quantity: 1,
			unit: '',
			source: '',
			sourceName: '',
			shape: '',
			extractionOrganName: '',
			extractionPersonName: '',
			extractionDateTime: '',
			extractMethod: '',
			extractionParts: '',
			describe: '',
			category: '',
			status: 0,
			chengSe:"",
			xingHao:"",
			extractionOrganId:0,
			subCategoryId:"",
			subCategoryName:"",
			buyPrice:"",
			buyDate:null,
			remark:"",
			shapeLs:[]

		};
		const state = reactive({
			isShowDialog: false,
			options: { ajlbs: [], tqffs: [], tqbws: [], tqrs: [], tqdws: [], wzlys: [], wzlbs: [], wzdws: [], wzxzs: [], dics: [] },
			formInline: {
				..._bakFormInline
			},
		});  
		// 打开弹窗
		const openDialog = (row: any) => {
			if (row?.evidenceId) {
				if(row?.evidenceId == -1){ 
					state.formInline = {..._bakFormInline};
				}else{
					getEvidence(row.evidenceId).then((res: any) => {
						if (res.success) {
							Object.assign(state.formInline, res.data);
						}
					});
				}
				
			} else {
				Object.assign(state.formInline, row);
			}
			state.isShowDialog = true;
		};
		// 关闭弹窗
		const closeDialog = () => {
			state.isShowDialog = false;
		};
		// 取消
		const onCancel = () => {
			closeDialog();
		};
		initDic(state.options);
		async function onSubmit() {
			await refAddEvi.value.$refs['refAddEvidenceForm'].validate((valid) => {
				if (valid) {
					state.formInline.shape = state.formInline.shapeLs.toString(); 
					save(state.formInline).then((res) => {
						if (res.success) {
							state.isShowDialog = false;
							emit('reload');
							ElMessage({ message: '操作成功', type: 'success' });
						}
					});
				}
			});
		}
		const refAddEvi = ref();
		function initEvidence() {}
		return {
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			refAddEvi,
			initEvidence,
			...toRefs(state),
		};
	},
};
</script>
